const express = require('express')
const fss = require('../../tools/fss')
//获取服务器地址
const base = require('../../base')
const path = require('path')
const router = express.Router()


//@GET /api/image/getuserimg
//获取用户头像
router.get('/getuserimg', (req, res) => {
  const url = '/userimg'
  // 获取userimg文件夹下所有图片文件的路径
  fss.readDir(path.join(__dirname, '../../public/userimg/'))
    .then(response => {
      // 构造每个图片文件的URL
      const imageUrls = response.map(fileName => {
        //进行路径的拼接
        return `${base.baseIP}${url}/${fileName}`;
      });
      console.log(imageUrls)
      res.send(imageUrls)
    })
    .catch(err => res.status(400).json('读取文件夹下文件失败'))
})

module.exports = router